package com.hnnou.freshmenappserver.controller;

import com.hnnou.freshmenappserver.bean.Function;
import com.hnnou.freshmenappserver.service.FunctionService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController

@Api(tags = "功能接口")
public class FunctionController {
    @Autowired
    FunctionService functionService;
    @PostMapping("/function/list")
    @ResponseBody
    public Map<String, Object> list() {
        Map<String, Object> map = new HashMap<>();
        List<Function> list = functionService.findAll();
        if (list != null) {
            map.put("success", true);
            map.put("msg", "成功");
            map.put("list",list);

        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }

    @PostMapping("/function/findone")
    @ResponseBody
    public Map<String, Object> findOneById(@RequestBody Function function) {
        Map<String, Object> map = new HashMap<>();
        Function function1 = functionService.selectById(function.getId());
        if (function1 != null) {
            map.put("success", true);
            map.put("msg", "成功");
            map.put("data", function1);
        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }
    @PostMapping("/function/delete")
    @ResponseBody
    public Map<String, Object> delete(@RequestBody Function function) {
        Map<String, Object> map = new HashMap<>();
        int result = functionService.deleteById(function.getId());
        if(result>0){

            map.put("success", true);
            map.put("msg", "成功");
        } else{
            map.put("success", false);
            map.put("msg", "失败");
        }

        return map;
    }
    @PostMapping("/function/update")
    @ResponseBody
    public Map<String, Object> update(@RequestBody Function function) {
        Map<String, Object> map = new HashMap<>();
        int count  = functionService.update(function);
        if (count > 0) {
            map.put("success", true);
            map.put("msg", "成功");
        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }
    // 添加
    @PostMapping("/function/addone")
    @ResponseBody
    public Map<String, Object> insert(@RequestBody Function function) {
        Map<String, Object> map = new HashMap<>();
        int count = functionService.insert(function);
        if (count > 0) {
            map.put("success", true);
            map.put("msg", "成功");
        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }

}
